<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Application_Model_ActivityModel {

    private $filePath = '/xml/act.xml';

    public function getNewId($liste) {
        $v1 = 0;
        $v2 = 0;
        foreach ($liste as $l) {
            if ($l->hasAttribute("id")) {
                $v2 = $l->getAttribute('id');
                if ($v2 > $v1) {
                    $v1 = $v2;
                }
            }
        }
        return $v1 + 1;
    }

    public function readAll() {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//activity');

        $tabActivity = Array();
        foreach ($listDomElements as $node) {
            $activity = new Application_Model_Activity();
            $activity->setId($node->getAttribute('id'));

            $activity->setContent($dom->queryXpath('//activity[@id=' . $activity->getId() . ']/content')->current()->textContent);
            $activity->setOwner(
                    new Application_Model_People(
                            $dom->queryXpath('//activity[@id=' . $activity->getId() . ']/owner/name'
                            )->current()->textContent,
                            $dom->queryXpath('//activity[@id=' . $activity->getId() . ']/owner/firstName'
                            )->current()->textContent
                    )
            );

            $activity->setPicturepath($dom->queryXpath('//activity[@id=' . $activity->getId() . ']/picture/@url')->current()->textContent);
            $activity->setTitle($dom->queryXpath('//activity[@id=' . $activity->getId() . ']/title')->current()->textContent);
            $tabActivity[] = $activity;
        }

        return $tabActivity;
    }

    public function countAll() {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//activity');
        $nb = count($listDomElements);

        return $nb;

        $this->view->xml = $listDomElements;
        $this->view->xml_nb = $nb;
    }

    public function read($id) {
        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $activity = new Application_Model_Activity();

        if ($dom->queryXpath('//activity[@id=' . $id . ']') == null) {
            return null;
        }

        $activity->setId($id);
        $activity->setContent($dom->queryXpath('//activity[@id=' . $id . ']/content')->current()->textContent);
        $activity->setOwner(new Application_Model_People(
                        $dom->queryXpath('//activity[@id=' . $id . ']/owner/name')->current()->textContent,
                        $dom->queryXpath('//activity[@id=' . $id . ']/owner/firstName')->current()->textContent
        ));

        $activity->setPicturepath($dom->queryXpath('//activity[@id=' . $id . ']/picture/@url')->current()->textContent);
        $activity->setTitle($dom->queryXpath('//activity[@id=' . $id . ']/title')->current()->textContent);
        return $activity;
    }

    public function insert($activity) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $allActivities = $dom->getElementsByTagName('activities');
        $id = $activity->getId();
        if (!isset($id) || $id == null || $id == '') {
            $id = $this->getNewId($dom->getElementsByTagName('activity'));
        }
        $root = $dom->createElement('activity');
        $root->setAttribute('id', $id);
        $dom->appendChild($root);
        $title = $dom->createElement('title');
        $elemtext = $dom->createTextNode($activity->getTitle());
        $title->appendChild($elemtext);
        $root->appendChild($title);
        $owner = $dom->createElement('owner');
        $root->appendChild($owner);
        $own_name = $dom->createElement("name");
        $elemtext = $dom->createTextNode($activity->getOwner()->getName());
        $own_name->appendChild($elemtext);
        $own_firstname = $dom->createElement("firstName");
        $elemtext = $dom->createTextNode($activity->getOwner()->getFirstName());
        $own_firstname->appendChild($elemtext);
        $owner->appendChild($own_name);
        $owner->appendChild($own_firstname);
        $picture = $dom->createElement("picture");
        $picture->setAttribute('url', $activity->getPicturepath());
        $root->appendChild($picture);
        $content = $dom->createElement("content");
        $elemtext = $dom->createTextNode($activity->getContent());
        $content->appendChild($elemtext);
        $root->appendChild($content);

        $activities = $allActivities->item(0);
        $activities->appendChild($root);


        // $this->setElement($dom, $dom->root, $root);
        // Mise à jour du fichier source original
        $dom->save(APPLICATION_PATH . $this->filePath);
        //  echo APPLICATION_PATH . '/xml/ev.xml';
    }

    function delete($id) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $root = $dom->getElementsByTagName('activities')->item(0);
        $allActivities = $dom->getElementsByTagName('activity');
        foreach ($allActivities as $activity) {
            if ($activity->hasAttribute("id")) {
                $v2 = $activity->getAttribute('id');
                if ($v2 == $id) {
                    $root->removeChild($activity);
                    $dom->save(APPLICATION_PATH . $this->filePath);
                    return true;
                }
            }
        }
        return false;
    }

    public function edit($id, $activity) {
        if ($this->delete($id)) {
            $activity->setId($id);
            $this->insert($activity);
        }
    }

    public function getComments() {

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $comments = Array();
        foreach ($dom->queryXpath('/activities/comment') as $comment) {
            $com = new Application_Model_Comment();

            $com->setContent($comment->getElementsByTagName('content')->item(0)->textContent);
            $com->setDate($comment->getElementsByTagName('date')->item(0)->textContent);
            $name = $comment->getElementsByTagName('name')->item(0)->textContent;
            $firstName = $comment->getElementsByTagName('firstName')->item(0)->textContent;
            $author = new Application_Model_People($name, $firstName);
            $com->setAuthor($author);
            $com->setId($comment->getAttribute('id'));
            $comments[] = $com;
        }
        return $comments;
    }

    public function addComment($id, $comment) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $allActivities = $dom->getElementsByTagName('activities');
        $id = $comment->getId();
        if (!isset($id) || $id == null || $id == '') {
            $id = $this->getNewId($dom->getElementsByTagName('comment'));
        }
        $root = $dom->createElement('comment');
        $root->setAttribute('id', $id);
        $dom->appendChild($root);
        $date = $dom->createElement('date');
        $elemtext = $dom->createTextNode($comment->getDate());
        $date->appendChild($elemtext);
        $root->appendChild($date);
        $author_name = $dom->createElement("name");
        $elemtext = $dom->createTextNode($comment->getAuthor()->getName());
        $author_name->appendChild($elemtext);
        $root->appendChild($author_name);
        $author_firstname = $dom->createElement("firstName");
        $elemtext = $dom->createTextNode($comment->getAuthor()->getFirstName());
        $author_firstname->appendChild($elemtext);
        $root->appendChild($author_firstname);
        $content = $dom->createElement("content");
        $elemtext = $dom->createTextNode($comment->getContent());
        $content->appendChild($elemtext);
        $root->appendChild($content);

        $comments = $allActivities->item(0);
        $comments->appendChild($root);


        // $this->setElement($dom, $dom->root, $root);
        // Mise à jour du fichier source original
        $dom->save(APPLICATION_PATH . $this->filePath);
        //  echo APPLICATION_PATH . '/xml/ev.xml';
    }

    public function removeComment($id) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $allComments = $dom->getElementsByTagName('comment');
        foreach ($allComments as $comment) {
            if ($comment->hasAttribute("id")) {
                $v2 = $comment->getAttribute('id');
                if ($v2 == $id) {
                    $root = $comment->parentNode;
                    $root->removeChild($comment);
                    $dom->save(APPLICATION_PATH . $this->filePath);
                    return true;
                }
            }
        }
        return false;
    }
    public function addActivities($tabActivity1, $tabActivity2) {
        $found = false;
        foreach ($tabActivity2 as $activity2) {
            foreach ($tabActivity1 as $activity1) {
                if ($activity2->getId() == $activity1->getId()) {
                    $activity1->increasePertinence();
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                $tabActivity1[] = $activity2;
            }
            $found = false;
        }
        return $tabActivity1;
    }

    public function search($key) {
        
        $tabActivity = $this->searchByTitle($key);
        $tabContent = $this->searchByContent($key);
        $tabActivity = $this->addActivities($tabActivity, $tabContent);
        $tabAuthor = $this->searchByAuthor($key);
        $tabActivity = $this->addActivities($tabActivity, $tabAuthor);
        $tabDate = $this->searchByDate($key);
        $tabActivity = $this->addActivities($tabActivity, $tabDate);
        usort($tabActivity, function( Application_Model_Activity $a, Application_Model_Activity $b) {
                    $a = $a->getPertinence();
                    $b = $b->getPertinence();
                    if ($a == $b) {
                        return 0;
                    }
                    return ($a < $b) ? -1 : 1;
                });
        return $tabActivity;
    }

    public function searchByTitle($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//activity[./title[contains(.,\'' . $key . '\')]]');
        return $this->getActivities($dom, $listDomElements);
    }

    public function searchByContent($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//activity[./content[contains(.,\'' . $key . '\')]]');
        return $this->getActivities($dom, $listDomElements);
    }

    public function searchByAuthor($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//activity[./owner/name[contains(.,\'' . $key . '\')] or ./owner/firstname[contains(.,\'' . $key . '\')]]');
        return $this->getActivities($dom, $listDomElements);
    }

    public function searchByDate($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//activity[./date[contains(.,\'' . $key . '\')]]');
        return $this->getActivities($dom, $listDomElements);
    }

    public function getActivities($dom, $listDomElements) {

        $tabActivity = Array();
        foreach ($listDomElements as $node) {
            $activity = new Application_Model_Activity();
            $activity->setId($node->getAttribute('id'));

            $activity->setContent($dom->queryXpath('//activity[@id=' . $activity->getId() . ']/content')->current()->textContent);
            $activity->setOwner(new Application_Model_People($dom->queryXpath('//activity[@id=' . $activity->getId() . ']/owner/name')->current()->textContent, $dom->queryXpath('//activity[@id=' . $activity->getId() . ']/owner/firstName')->current()->textContent));

            $activity->setPicturepath($dom->queryXpath('//activity[@id=' . $activity->getId() . ']/picture/@url')->current()->textContent);
            $activity->setTitle($dom->queryXpath('//activity[@id=' . $activity->getId() . ']/title')->current()->textContent);
            $tabActivity[] = $activity;
        }

        return $tabActivity;
    }

}

?>
